package com.vip.jwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vip.jwt.entity.*;
import com.vip.jwt.mapper.RoleMapper;
import com.vip.jwt.mapper.RoleMenuMapper;
import com.vip.jwt.mapper.RolePermissionMapper;
import com.vip.jwt.service.RoleMenuService;
import com.vip.jwt.service.RolePermissionService;
import com.vip.jwt.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author vip
 * @since 2021-05-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Resource
    private RolePermissionService rolePermissionService;

    @Override
    public int saveRole(Role role) {
        roleMapper.insert(role);
        List<Menu> menus = role.getMenus();
        if(menus.size()>0){
            List<String> menuIds = menus.stream().map(Menu::getId).collect(Collectors.toList());
            menuIds.forEach(e->{
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getId());
                roleMenu.setMenuId(e);
                roleMenuMapper.insert(roleMenu);
            });
        }
        List<Permission> permissions = role.getPermissions();
        if(permissions.size()>0){
            List<String> permissionIds = permissions.stream().map(Permission::getId).collect(Collectors.toList());
            permissionIds.forEach(e->{
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setPermissionId(e);
                rolePermissionMapper.insert(rolePermission);
            });
        }
        return 0;
    }

    @Override
    public int deleteById(String id) {
        roleMapper.deleteById(id);
        roleMenuService.deleteByRoleId(id);
        rolePermissionService.deleteByRoleId(id);
        return 0;
    }

    @Override
    public int updateRole(Role role) {
        roleMapper.updateById(role);
        //删除角色菜单中间表
        roleMenuService.deleteByRoleId(role.getId());
        List<Menu> menus = role.getMenus();
        if (menus.size() > 0) {
            //更新角色菜单中间表
            List<String> menuIds = menus.stream().map(Menu::getId).collect(Collectors.toList());
            menuIds.forEach(e -> {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getId());
                roleMenu.setMenuId(e);
                roleMenuMapper.insert(roleMenu);
            });
        }
        //删除角色权限中间表
        rolePermissionService.deleteByRoleId(role.getId());
        List<Permission> permissions = role.getPermissions();
        if (permissions.size() > 0) {
            //更新角色权限中间表
            List<String> permissionIds = permissions.stream().map(Permission::getId).collect(Collectors.toList());
            permissionIds.forEach(e -> {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setPermissionId(e);
                rolePermissionMapper.insert(rolePermission);
            });
        }
        return 0;
    }

}
